home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / whisper / source / snd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  5.6 KB  |  287 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <snd.h>
  5.  
  6. #define    TRUE    1
  7. #define    FALSE    0
  8. #define    ERR    (-1)
  9.  
  10. typedef struct _MP {
  11.     struct _MP    *next;
  12.     char    *file;
  13.     char    *buf;
  14. } MEM_FP;
  15.  
  16. void    macset(char *mac,char *str);
  17. char    *MEM_lzss(FILE *fp);
  18. char    *strdup(char *str);
  19.  
  20. extern char    swork[];
  21.  
  22. static char    *snd_buf=NULL;
  23. static char    *eup_buf=NULL;
  24. static int    eup_time=0;
  25. static MEM_FP    *top_mp=NULL;
  26.  
  27. char    *getins(env,file)
  28. char    *env,*file;
  29. {
  30.     static char tmp[128];
  31.     char *p;
  32.  
  33.     if ( (p = getenv(env)) == NULL )
  34.     return NULL;
  35.     sprintf(tmp,"%s\\%s",p,file);
  36.     return tmp;
  37. }
  38. char    *xopen(char *file)
  39. {
  40.     FILE    *fp;
  41.     long    fsz;
  42.     char    *buf;
  43.     register MEM_FP  *mp;
  44.  
  45.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  46.     if ( strcmp(mp->file,file) == 0 )
  47.         return mp->buf;
  48.     }
  49.  
  50.     if( (fp = fopen(file,"rb")) == NULL )
  51.     return NULL;
  52.  
  53.     if ( (buf = MEM_lzss(fp)) != NULL ) {
  54.     fclose(fp);
  55.     return buf;
  56.     }
  57.  
  58.     fseek(fp,0L,SEEK_END);
  59.     fsz = ftell(fp);
  60.     rewind(fp);
  61.  
  62.     if( (buf = malloc(fsz)) == NULL) {
  63.     fclose(fp);
  64.     return NULL;
  65.     }
  66.     fread(buf,fsz,1,fp);
  67.     fclose(fp);
  68.     return buf;
  69. }
  70. void    xclose(char *buf)
  71. {
  72.     register MEM_FP  *mp;
  73.  
  74.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  75.     if ( buf == mp->buf )
  76.         return;
  77.     }
  78.     free(buf);
  79. }
  80. int    mem_file(char *file)
  81. {
  82.     register MEM_FP  *mp;
  83.  
  84.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  85.     if ( strcmp(mp->file,file) == 0 )
  86.         return TRUE;
  87.     }
  88.  
  89.     if ( (mp = (MEM_FP *)malloc(sizeof(MEM_FP))) == NULL )
  90.     return ERR;
  91.  
  92.     if ( (mp->buf = xopen(file)) == NULL ) {
  93.     free(mp);
  94.     return ERR;
  95.     }
  96.  
  97.     mp->file = strdup(file);
  98.     mp->next = top_mp;
  99.     top_mp = mp;
  100.     return FALSE;
  101. }
  102. void    unmem_file(char *file)
  103. {
  104.     register MEM_FP  *mp;
  105.     register MEM_FP  *op;
  106.  
  107.     for ( mp = top_mp ; mp != NULL ; mp = mp->next ) {
  108.     if ( strcmp(mp->file,file) == 0 ) {
  109.         if ( mp == top_mp )
  110.         top_mp = mp->next;
  111.         else
  112.         op->next = mp->next;
  113.         free(mp->buf);
  114.         free(mp);
  115.         break;
  116.     }
  117.     op = mp;
  118.     }
  119. }
  120. void    PLAY_snd(file)
  121. char    *file;
  122. {
  123.     if ( snd_buf != NULL || eup_buf != NULL )
  124.     return;
  125.  
  126.     SND_pcm_sound_delete(-1);
  127.     SND_pcm_mode_set(1);
  128.  
  129.     if ( (snd_buf = xopen(file)) == NULL )
  130.     return;
  131.  
  132.     SND_pan_set(71,64);
  133.     *(int *)(snd_buf+20) = 0;
  134.     SND_pcm_play(71,snd_buf[28],127,snd_buf);
  135.  
  136.     macset("SND",file);
  137. }
  138.  
  139. void    CHK_snd(void)
  140. {
  141.     if ( snd_buf != NULL && !SND_pcm_status(71) ) {
  142.     SND_pcm_play_stop(71);
  143.     SND_pcm_rec_stop();
  144.     xclose(snd_buf);
  145.     snd_buf = NULL;
  146.     macset("SND","");
  147.     }
  148. }
  149.  
  150. void    PLAY_eup(file)
  151. char    *file;
  152. {
  153.     int     i;
  154.     int     err,size,signa,tempo;
  155.     char    *p;
  156.     char    tmp[16];
  157.     char    dmy[16];
  158.     char    wrk[80];
  159.  
  160.     if ( snd_buf != NULL || eup_buf != NULL )
  161.     return;
  162.  
  163.     if( (eup_buf = xopen(file)) == NULL )
  164.     return;
  165.  
  166.     SND_eup_init(swork);
  167.     SND_elevol_mute(0xB3);
  168.  
  169.     p = &eup_buf[852];        /* trk mute */
  170.     for( i = 0 ; i < 32 ; i++ )
  171.     err = SND_eup_mute_set(i,*(p++));
  172.  
  173.     p = &eup_buf[884];        /* trk port */
  174.     for( i = 0 ; i < 32 ; i++ )
  175.     err = SND_eup_port_set(i,*(p++));
  176.  
  177.     p = &eup_buf[916];        /* trk midi ch */
  178.     for( i = 0 ; i < 32 ; i++ )
  179.     err = SND_eup_midi_ch_set(i,*(p++));
  180.  
  181.     p = &eup_buf[948];        /* trk key bias */
  182.     for( i = 0 ; i < 32 ; i++ )
  183.     err = SND_eup_bias_set(i,*(p++));
  184.  
  185.     p = &eup_buf[980];        /* trk transpose */
  186.     for( i = 0 ; i < 32 ; i++ )
  187.     err = SND_eup_transpose_set(i,*(p++));
  188.  
  189. /********************
  190.     channel assign
  191. *********************/
  192.  
  193.     p = &eup_buf[1748];        /* fm midi ch */
  194.     for( i = 0 ; i < 6 ; i++ )
  195.     err = SND_midi_ch_assign(i,*(p++));
  196.  
  197.     p = &eup_buf[1754];        /* pcm midi ch */
  198.     for( i = 0 ; i < 8 ; i++ )
  199.     err = SND_midi_ch_assign(i+64,*(p++));
  200.  
  201. /****************
  202.     bank load
  203. *****************/
  204.  
  205.     SND_pcm_mode_set(0);
  206.  
  207.     strncpy(tmp,&eup_buf[1762],8);    /* fm file name */
  208.     tmp[8] = '\0';
  209.     if ( tmp[0] != '\0' ) {
  210.     strcat(tmp,".FMB");
  211.     strcpy(wrk,file);
  212.     if ( (p = strrchr(wrk,'\\')) != NULL ) {
  213.         strcpy(p+1,tmp);
  214.         p = wrk;
  215.     } else
  216.         p = tmp;
  217.     p = tmp;
  218.         if ( SND_fm_bank_load(p,dmy) != 0 ) {
  219.         if ( (p = getins("FMINST",tmp)) != NULL )
  220.          SND_fm_bank_load(p,dmy);
  221.     }
  222.     }
  223.  
  224.     strncpy(tmp,&eup_buf[1770],8);    /* pcm file name */
  225.     tmp[8] = '\0';
  226.     if ( tmp[0] != '\0' ) {
  227.     strcat(tmp,".PMB");
  228.     strcpy(wrk,file);
  229.     if ( (p = strrchr(wrk,'\\')) != NULL ) {
  230.         strcpy(p+1,tmp);
  231.         p = wrk;
  232.     } else
  233.         p = tmp;
  234.     p = tmp;
  235.         if ( SND_pcm_bank_load(p,dmy) != 0 ) {
  236.         if ( (p = getins("PCMINST",tmp)) != NULL )
  237.          SND_pcm_bank_load(p,dmy);
  238.     }
  239.     }
  240.  
  241. /*******************
  242.     play eup file
  243. ********************/
  244.  
  245.     p = &eup_buf[2048];        /* data top */
  246.     size = *((int *)p); p += 4;
  247.     signa = *(p++);
  248.     tempo = *(p++);
  249.  
  250.     SND_eup_loop_set(0);
  251.     SND_eup_tempo_set(tempo);
  252.     SND_eup_play_start(p,size,signa);
  253.  
  254.     macset("EUP",file);
  255. /*****************************
  256.     while ( SND_eup_stat_meas() < 1 );
  257. *****************************/
  258.     eup_time = 500;
  259. }
  260.  
  261. void    CHK_eup(void)
  262. {
  263.     if ( eup_buf != NULL && !SND_eup_stat_flag() ) {
  264.     if ( --eup_time > 0 )
  265.         return;
  266.     SND_eup_play_stop();
  267.     SND_eup_end();
  268.     xclose(eup_buf);
  269.     eup_buf = NULL;
  270.     if ( macval("EUP_REPLAY_FLG") == 0 )
  271.         macset("EUP","");
  272.     else
  273.         MSG_run(prcget("endof_eup"));
  274.     }
  275. }
  276.  
  277. void    END_eup(void)
  278. {
  279.     if ( eup_buf != NULL ) {
  280.     SND_eup_play_stop();
  281.     SND_eup_end();
  282.     xclose(eup_buf);
  283.     eup_buf = NULL;
  284.     macset("EUP","");
  285.     }
  286. }
  287.